﻿using System;
using System.Security.Principal;

namespace Portal.Api
{
  /// <summary>
  /// Zusammenfassungsbeschreibung für Principal
  /// </summary>
  public class Principal : System.Security.Principal.IPrincipal
  {
    private IIdentity _identity;
    private string[] _roles;

    public Principal(IIdentity identity, string[] roles)
    {
      Id = Guid.Empty;
      _identity = identity;
      _roles = new string[roles.Length];
      Roles = roles;
    }

    public bool IsInRole(string role)
    {
      return Array.BinarySearch(_roles, role) >= 0 ? true : false;
    }
    public IIdentity Identity
    {
      get
      {
        return _identity;
      }
    }


    #region Properties
    public Guid Id
    {
      get;
      set;
    }
    public string Login
    {
      get
      {
        return _identity.Name;
      }
    }
    public string FirstName
    {
      get;
      set;
    }
    public string LastName
    {
      get;
      set;
    }
    public string FullName
    {
      get
      {
        return FirstName + " " + LastName;
      }
    }
    public string Email
    {
      get;
      set;
    }
    public string[] Roles
    {
      get
      {
        return _roles;
      }
      set
      {
        value.CopyTo(_roles, 0);
        Array.Sort(_roles);
      }
    }
    #endregion
  }
}